import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import { isLogined } from "../Uitils/auth";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
  {
    path: "/list",
    name: "List",
    component: () => import("../views/List.vue"),
  },
  {
    path: "/user",
    name: "User",
    component: () => import("../views/User.vue"),
    meta: {
      needLogin: true,
      requiresAuth: true,
    },
  },
  {
    path: "/list/:id/:name?",
    name: "Detail",
    component: () => import("../views/Detail.vue"),
    meta: {
      hideTabBar: true, // 是否隐藏底部导航
    },
  },

  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/car",
    name: "Car",
    component: () => import("../views/Car.vue"),
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("../views/Reg.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    name: "Search",
    path: "/search",
    component: () => import("../views/Search.vue"),
  },
  {
    path: "/list/:id/:name?/appraise",
    name: "Appraise",
    component: () => import("../views/Appraise.vue"),
    meta: {
      hideTabBar: true,
    },
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // 是否登录
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});
export default router;
